package com.jedisdemo;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;

import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

/**
 * Created by dong peng on 2017/8/15.
 */
public class JedisTest {

    Jedis jedis = null;

    @Before
    public void testConfig() {
        this.jedis = new Jedis("19.19.22.54",6379);
        jedis.auth("_@Fintell");
//        this.jedis = new Jedis("127.0.0.1",6379);
    }

    @Test
    public void testpipeLine() {
       jedis.select(1);
        Pipeline pipeline = jedis.pipelined();
        pipeline.multi();
        pipeline.set("test2","456");
        pipeline.set("test1","123");
        pipeline.exec();
        pipeline.sync();

        Response<String> response = pipeline.get("test1");
        Response<String> response1 = pipeline.get("test2");
        pipeline.sync();
        System.out.println(response.get());
        System.out.println(response1.get());
//        System.out.println(jedis.get("test1"));
    }

    @Test
    public void testKeys() {
        Jedis conn = JedisPoolUtil.getInstance();
        Set sets = conn.keys("*");
        System.out.println(sets.size());
        System.out.println(sets);
    }

    @Test
    public void testSetGet() {
        for( int i = 0; i < 10000; i++) {
            jedis.set("foo", "bar");
            String value = jedis.get("foo");
            String name = jedis.get("name");
            Set set = jedis.keys("*");

            System.out.println("value: " +value);
            System.out.println("name: "+ name);
            System.out.println("set: "+ set);

        }
    }

    @Test
    public  void hset() {
        jedis.hset("myhash","name","li");
        jedis.hset("myhash","age","18");
        Map map = jedis.hgetAll("myhash");
        System.out.println(map);
    }

    @Test
    public  void testTransaction() {

        for(int i = 0; i < 5; i++) {
        new Thread(new Runnable() {
            public void run() {

                    System.out.println("取出数据"+jedis);
                    String str = jedis.get("key");
                    System.out.println("取出数据"+str);
                    str = str == null ? "0" : str;
                    int v = Integer.parseInt(str);
                    v++;
                    jedis.set("key",String.valueOf(v));


            }
        }).start();
        }
    }


    /**
     * 测试分布式锁
     */
    @Test
    public void  testLock() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(6);
        for (int i= 0; i< 6; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String key = "com";
                    String value = UUID.randomUUID().toString();
                    Jedis jedis = JedisPoolUtil.getInstance();
                    System.out.println(">>>>>>开始"+Thread.currentThread().getName());
                    boolean r = false;
                    do{
                        r = LockByRedis.getLock(jedis,key,value,10000);
                        if(r) {
                            System.out.println("获得锁成功---------"+Thread.currentThread().getName());
                        }
                    }while (!r);


                    if(LockByRedis.releaseLock(jedis,key,value)){
                        System.out.println("释放锁"+ Thread.currentThread().getName());
                    }
                    latch.countDown();
                }
            }).start();

        }


        latch.await();
    }


    @Test
    public void  testGetSeqNo() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(6);
        for (int i= 0; i< 6; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String key = "com";
                    Jedis jedis = JedisPoolUtil.getInstance();
                    long str =jedis.incr(key);
                    System.out.println("key"+str);
                    latch.countDown();
                }
            }).start();

        }


        latch.await();
    }
}
